import axios from 'axios'
import React,{Component} from "react";

export default class GetList extends Component{
    state={dataList:[],isLoading:false}

   getList=(flag)=>{
        let url=flag==='1'?'server1':'server2'
       this.setState({isLoading:true})
       setTimeout(()=>{
           axios.get(url+'/getList').then(
               res=>{
                   this.setState({isLoading:false})
                   return res.data
               },
               err=>{
                   return Promise.reject(err)
               }
           ).then(res=>{
               this.setState({dataList:res.data})
           })
       },2000)
   }
    render(){
        const {dataList,isLoading}=this.state
        return(
            <div>
              <button onClick={()=>this.getList('1')}>server1</button>
              <button onClick={()=>this.getList('2')}>server2</button>
                <ul>
                    {isLoading?'加载中....':dataList.length===0?'我是首页，欢迎您':dataList.map(item=><li key={item.name}>{item.name}</li>)}
                </ul>
            </div>
        )
    }
}
